﻿using System;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;

namespace Check
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public static int GetPidByProcessName(string processname)
        {
            Process[] ProcessNames = Process.GetProcessesByName(processname);
            //foreach (var item in ProcessNames)
            //{
            //    return item.Id;
            //}
            Process[] array = ProcessNames;
            if (array.Length > 0)
            {
                Process proce = array[0];
                return proce.Id;
            }
            return 0;
        }
        Timer timer = null;
        private void button1_Click(object sender, EventArgs e)
        {
            string name = this.txtName.Text;
            string path = this.txtPath.Text;

            if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(path)||!File.Exists(path))
            {
                MessageBox.Show("请填写进程名称和程序路径，确保路径真实存在.");
            }
            else
            {
                timer = new Timer();
                timer.Interval = 3000;
                timer.Tick += Timer_Tick;
                timer.Start();
                MessageBox.Show("启动成功");
            }
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            try
            {
                string name = this.txtName.Text;
                string path = this.txtPath.Text;

                if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(path))
                {
                    if (GetPidByProcessName(name) > 0)
                    {
                        Console.WriteLine("启动");
                    }
                    else
                    {
                        Console.WriteLine("未启动");
                        if (path.Contains(".exe"))
                        {
                            Process.Start(path, "");
                        }
                        else
                        {
                            Process pro = new Process();
                            FileInfo file = new FileInfo(txtPath.Text);
                            pro.StartInfo.WorkingDirectory = file.Directory.FullName;
                            pro.StartInfo.FileName = txtPath.Text;
                            pro.StartInfo.CreateNoWindow = false;
                            pro.Start();
                        }


                        FileLogger.LogInfo("启动一次" + this.txtName.Text + "程序\r\n");
                    }
                }
            }
            catch (Exception ex)
            {
                FileLogger.LogError("进程监控", ex);
                if (timer != null) timer.Stop();
            }
           
        }
        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = "D:\\Program Files (x86)";
            openFileDialog.Filter = "exe文件|*.exe|所有文件|*.*";
            openFileDialog.RestoreDirectory = true;
            openFileDialog.FilterIndex = 1;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                this.txtPath.Text= openFileDialog.FileName;
            }
        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.ShowInTaskbar = false;
                notifyIcon1.Visible = true;
            }
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Show();
                this.ShowInTaskbar = true;
                this.WindowState = FormWindowState.Normal;
                notifyIcon1.Visible = false;
            }
        }

        private void 显示窗口ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Show();
                this.ShowInTaskbar = true;
                this.WindowState = FormWindowState.Normal;
                notifyIcon1.Visible = false;
            }
        }

        private void 退出系统ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            notifyIcon1.Visible = false;
            this.Close();
            this.Dispose(true);

        }
    }
}
